<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_COST_FLIER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_DM,$login['staff_auth']);

$dao = new Class_M_COST_FLIER;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$matrix_mst = Class_M_OTHER::get_by_matrix_cd( $_GET['m'] );
		$forms  = $dao->get_valuesDM( $_GET['id'], $_GET['m'],$matrix_mst["pre_matrix_cd"]);
		
		$forms["matrix_cd"] = $_GET['m'];
		Class_PWD::set_rate_inc($forms);
		
		//$forms["id"] = $_GET['id'];
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
	}else{
		//$forms['auth'] = 0;
	}
	
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
/*
		if( $err_mes['pattern_name'] == '' ){

			if( $forms['id'] == '' ){
				$where = sprintf(" pattern_name = '%s'", $forms['pattern_name'] );
			}else{
				$where = sprintf(" pattern_name = '%s' AND id <> %s", $forms['pattern_name'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['pattern_cd'] = '<p class="error">このIDは重複しています</p>';
			}
		}
*/
		if ( $err->clear ){
			/*$cols     = $dao->get_cols();
			
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			*/
			
			$values["plane_dm"] = $forms["plane_cost"] ;
			$values["shop_cd"] = $forms["shop_cd"] ;
			$values["matrix_cd"] = $forms["matrix_cd"] ;
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}

		if ( $err->clear ){
			if( $forms['id'] == '' ){

				$where = sprintf("shop_cd = '%s' AND matrix_cd = '%s' ", $forms['shop_cd'], $forms['matrix_cd'] );
			}else{
				$where = sprintf("shop_cd = '%s' AND matrix_cd = '%s' AND id  <> %s", $forms['shop_cd'], $forms['matrix_cd'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['shop_cd'] = '<p class="error">このデータは重複しています</p>';
			}
		}
		
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$matrix_mst = Class_M_OTHER::get_by_matrix_cd( $forms["matrix_cd"] );
			$phase = 'input';
		}
	}
}



$forms["weekname"] = Class_PWD::get_week_name($matrix_mst, true );

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {

}
$dspflags[""] = "すべて";
foreach ($aoki_disp_flags as $k => $v) {
	$dspflags[$k] = $v;
}

$smarty->assign('dspflags', $dspflags );

$smarty->assign('flags', $aoki_pattern_flags );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mcost_dm_plane_form.html');
?>